package com.wcy.entity.pageVo;

import com.wcy.annotation.pageVo.MappingColumn;
import com.wcy.annotation.pageVo.PageColumn;
import com.wcy.annotation.pageVo.SelectionAnnotation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
//表结构的单元，前端通过处理这个数据来还原后端数据
public class Column{
    //指向这个列的list获取api，以便生成select选择器，checkbox，radio等
    @AllArgsConstructor
    @Data
    public static class SelectionReference{
        private String apiUrl;
        private String mapProp;
        private String mapLabel;
        public SelectionReference(SelectionAnnotation selectionAnnotation){
            apiUrl = selectionAnnotation.apiUrl();
            mapProp = selectionAnnotation.mapProp();
            mapLabel = selectionAnnotation.mapLabel();
        }
    }
    @AllArgsConstructor
    @Data
    public static class MappingReference{
        private String targetProp;
        private String mappingByPropUrl;
        private String mappingListUrl;
        private String mappingListProp;
        private String mappingListLabel;
        public MappingReference(MappingColumn mappingColumn){
            targetProp = mappingColumn.targetProp();
            mappingByPropUrl = mappingColumn.mappingByPropUrl();
            mappingListUrl = mappingColumn.mappingListUrl();
            mappingListProp = mappingColumn.mappingListProp();
            mappingListLabel = mappingColumn.mappingListLabel();
        }
    }
    private Boolean isId;//是否是主键
    private String prop;//键名称
    private String label;//前端显示名称
    private String type;//列类型
    private String clickUrlPrefix;//点击当前列时的跳转路径，否则默认为空
    private Boolean allowEdit;//是否允许编辑
    private SelectionReference selectRef;//当type为select、radio、checkbox等类型时用于获取数据，否则默认为空
    private MappingReference mappingRef;//当type为mapping时用于指向映射的list，否则默认为空
    private String url;//当type为img时用于指向要上传的路径，否则默认为空
    private String fetchUrl;//当type为img时用于指向要获取的路径，否则默认为空
    public Column(String prop, String label, String type, Boolean allowEdit) {
        this.prop = prop;
        this.label = label;
        this.type = type;
        this.allowEdit = allowEdit;
    }

    public Column(PageColumn pageColumn){
        this.isId = pageColumn.isId();
        this.prop = pageColumn.prop();
        this.label = pageColumn.label();
        this.type = pageColumn.type();
        this.allowEdit = pageColumn.allowEdit();
        this.url = pageColumn.url();
        this.clickUrlPrefix = pageColumn.clickUrlPrefix();
        this.fetchUrl = pageColumn.fetchUrl();
        this.selectRef = new SelectionReference(pageColumn.selectRef());
        this.mappingRef = new MappingReference(pageColumn.mappingRef());
    }

    public void overrideColumn(Column column){
        if(column.getLabel()!=null&& !column.getLabel().isEmpty())
            this.label = column.getLabel();
        if(column.getType()!=null&& !column.getType().isEmpty())
            this.type = column.getType();
        if(column.getAllowEdit()!=null)
            this.allowEdit = column.getAllowEdit();
        if(column.getUrl()!=null&&  !column.getUrl().isEmpty())
            this.url = column.getUrl();
        if(column.getClickUrlPrefix()!=null&&  !column.getClickUrlPrefix().isEmpty())
            this.clickUrlPrefix = column.getClickUrlPrefix();
        if(column.getSelectRef()!=null) {
            if(this.selectRef==null)
                this.selectRef = column.getSelectRef();

            if(column.getSelectRef().getApiUrl()!=null&& !column.getSelectRef().getApiUrl().isEmpty())
                this.selectRef.setApiUrl(column.getSelectRef().getApiUrl());
            if(column.getSelectRef().getMapProp()!=null&& !column.getSelectRef().getMapProp().isEmpty())
                this.selectRef.setMapProp(column.getSelectRef().getMapProp());
            if(column.getSelectRef().getMapLabel()!=null&& !column.getSelectRef().getMapLabel().isEmpty())
                this.selectRef.setMapLabel(column.getSelectRef().getMapLabel());
        }
        if(column.getMappingRef()!=null){
            if(this.mappingRef==null)
                this.mappingRef = column.getMappingRef();

            if(column.getMappingRef().getTargetProp()!=null&& !column.getMappingRef().getTargetProp().isEmpty())
                this.mappingRef.setTargetProp(column.getMappingRef().getTargetProp());
            if(column.getMappingRef().getMappingByPropUrl()!=null&&  !column.getMappingRef().getMappingByPropUrl().isEmpty())
                this.mappingRef.setMappingByPropUrl(column.getMappingRef().getMappingByPropUrl());
            if(column.getMappingRef().getMappingListUrl()!=null&&  !column.getMappingRef().getMappingListUrl().isEmpty())
                this.mappingRef.setMappingListUrl(column.getMappingRef().getMappingListUrl());
            if(column.getMappingRef().getMappingListProp()!=null&&  !column.getMappingRef().getMappingListProp().isEmpty())
                this.mappingRef.setMappingListProp(column.getMappingRef().getMappingListProp());
            if(column.getMappingRef().getMappingListLabel()!=null&&  !column.getMappingRef().getMappingListLabel().isEmpty())
                this.mappingRef.setMappingListLabel(column.getMappingRef().getMappingListLabel());
        }
        if(column.getFetchUrl()!=null&& !column.getFetchUrl().isEmpty())
            this.fetchUrl = column.getFetchUrl();
        if(column.getIsId()!=null)
            this.isId = column.getIsId();
    }
}
